package com.george.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.george.model.dto.ResourceListDto;
import com.george.model.dto.ResourceTreeDto;
import com.george.model.entity.Resources;
import com.george.dao.ResourcesMapper;
import com.george.model.query.ResourceQueryDto;
import com.george.service.IResourcesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 菜单资源表 服务实现类
 * </p>
 *
 * @author George Chan
 * @since 2019-09-28
 */
@Service
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements IResourcesService {
    private final Logger logger = LoggerFactory.getLogger(ResourcesServiceImpl.class);

    @Override
    public boolean saveResouceData(Resources resources) {
        if (ObjectUtil.isNull(resources.getParentId())) {
            resources.setParentId(0L);
        }
        return this.baseMapper.insert(resources) > 0;
    }

    @Override
    public PageInfo<ResourceListDto> findResouceList(Integer page, Integer limit, ResourceQueryDto queryDto) {
        PageHelper.startPage(page-1, limit);
        List<ResourceListDto> resources = this.baseMapper.getResourceList(queryDto);
        if (CollectionUtil.isEmpty(resources)) {
            resources = Collections.emptyList();
        }
        PageInfo<ResourceListDto> pageInfo = new PageInfo<>(resources);
        pageInfo.setList(resources);
        return pageInfo;
    }

    @Override
    public List<ResourceTreeDto> findResourceTreeData(String name) {
        List<ResourceTreeDto> treeDtos = this.baseMapper.getAllResourceList(name);
        if (CollectionUtil.isEmpty(treeDtos)) {
            treeDtos  = Collections.emptyList();
        }
        List<ResourceTreeDto> rootTrees = Lists.newArrayList();
        for (ResourceTreeDto treeNode : treeDtos) {
            treeNode.setSpread(true);
            if(StringUtils.equals(treeNode.getParentId(), "0")){
                rootTrees.add(treeNode);
            }
            for (ResourceTreeDto t : treeDtos) {
                if(StringUtils.equals(String.valueOf(t.getParentId()),treeNode.getId())){
                    if(treeNode.getChildren() == null){
                        List<ResourceTreeDto> myChildrens = Lists.newArrayList();
                        myChildrens.add(treeNode);
                        treeNode.setChildren(myChildrens);
                    }
                    treeNode.getChildren().add(t);
                }
            }
        }
        return rootTrees;
    }

    /**
     * 对象转换
     * @param data 对象
     * @return 要转换成为的对象
     */
    private ResourceTreeDto resourceToTree(Resources data) {
        ResourceTreeDto treeDto = new ResourceTreeDto();
        treeDto.setTitle(data.getName());
        treeDto.setCode(data.getCode());
        treeDto.setId(data.getId()+"");
        treeDto.setParentId(data.getParentId()+"");
        treeDto.setSpread(true);
        return treeDto;
    }
}
